package com.weishi.project.speedometer.domain;

import com.weishi.framework.config.BeanContext;
import com.weishi.project.speedometer.service.ISpeedometerService;
import com.weishi.project.speedometer.service.IWitService;
import com.weishi.project.speedometer.vo.MqttDataVo;
import com.weishi.project.training.domain.WitAccLog;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttMessage;

import java.util.logging.Logger;

public class WitCallBack implements MqttCallBackCommon {

    public String topic = "wit_acc";

    private ISpeedometerService speedometerService;

    private IWitService witService;

    public WitCallBack() {
        this.speedometerService = BeanContext.getApplicationContext().getBean(ISpeedometerService.class);
        this.witService = BeanContext.getApplicationContext().getBean(IWitService.class);
    }

    @Override
    public void connectionLost(Throwable throwable) {

    }

    Logger logger = Logger.getLogger("WitCallBack");

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {
        String message = new String(mqttMessage.getPayload());
        try {
            String[] datas = message.split("/");
            MqttDataVo mqttDataVo = new MqttDataVo();
            mqttDataVo.setType(topic);
            mqttDataVo.setDevId(datas[0]);
            mqttDataVo.setTimeTimp(datas[2]);
            mqttDataVo.setDatas(datas);
            speedometerService.addRealTimeData(mqttDataVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }

    @Override
    public String getTopic() {
        return topic;
    }
}
